<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function zbcjson($data = true, $msg = '', $code = 0, $httpState = 200)
{
    return json(array(
        "code" => $code,
        "msg" => $msg,
        "data" => $data
    ), $httpState);
}

function pagejson($data, $count, $msg = "", $code = 0, $httpState = 200)
{
    return json(array(
        "count" => $count,
        "code" => $code,
        "msg" => $msg,
        "data" => $data
    ), $httpState);
}

/**
 * 对称加密
 */
function encrypt($data)
{
    $method = config('aes.method');
    $key = config('aes.key');
    $iv = config('aes.iv');
    $options = config('aes.options');
    return openssl_encrypt($data, $method, $key, $options, $iv);
}

/**
 * 对称解密
 */
function decrypt($data)
{
    $method = config('aes.method');
    $key = config('aes.key');
    $iv = config('aes.iv');
    $options = config('aes.options');
    return openssl_decrypt($data, $method, $key, $options, $iv);
}

function http_mergeParams($post){
    $data=[];
    foreach($post as $key=>$value){
        $index=0;
        $temp=http_resolveParam($key,$value,$index);
        array_mergeParam($data,$temp);
        $index++;
    }
    return $data;
}

function http_resolveParam($field,$params,$index=null,&$data=[]){
    $result=[];
    if(!is_array($params)) {
        if(is_null($index)){
            $result[$field]=$params;
        }else{
            $result[$index][$field]=$params;
        }
        return $result;
    }

    $index=0;
    foreach($params as $key=>$value){
        if(is_array($value)){
            $fields=http_resolveParam($field,$value,$index);
            foreach($fields as $fkey=>$fvalue){
                if(count($fvalue)>1){
                    foreach($fvalue as $subKey=>$subValue){
                        if(is_array($subValue)){
                            $result[$subKey][$key][$fkey][$field]=$subValue[$field];
                        }else{
                            $result[$fkey][$key][$field]=$fvalue[$field];       
                        }
                    }
                } else if(count($fvalue)==1){
                    foreach($fvalue as $subKey=>$subValue){
                        if(is_array($subValue)){
                            $result[$subKey][$key][$fkey][$field]=$subValue[$field];        
                        }else{
                            $result[$fkey][$key][$field]=$fvalue[$field];       
                        }
                    }
                }
                else
                {
                    foreach($fvalue as $subKey=>$subValue){
                        if(is_array($subValue)){
                            $result[$fkey][$key][$subKey][$field]=$subValue[$field];        
                        }else{
                            $result[$fkey][$key][$field]=$fvalue[$field];       
                        }
                    }
                    
                } 
            }  
        }else{
            $result[$key][$field]=$value;
        }
        $index++;
    }
    return $result;
}

function array_mergeParam(&$source,$target)
{
    if(!is_array($target)){
        if(!is_array($source)){
            $source=$target;
        }
        return;
    }

    foreach($target as $key=>$value){
        if(is_array($value)){
            array_mergeParam($source[$key],$value);
        }else{
            $source[$key]=$value;
        }
    }
}